﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nage.Algorithms;
using Nage.Algorithms.Population;
using Nage.Algorithms.Solution;
using Utils.Logger;
using Nage.Examples.Rastrigin.Solution;

namespace Nage.Examples.Rastrigin.Population
{
    /// <summary>
    /// Factory class responsible for creating random simple genome population
    /// </summary>
    public class CoordinatesPopulationFactory : IPopulationFactory<Coordinates>
    {
        #region Public properties
        /// <summary>
        /// Defines the size of population 
        /// </summary>
        public Int32 PopulationSize { get; set; }

        /// <summary>
        /// Solution factory
        /// </summary>
        public ISolutionFactory<Coordinates> SolutionFactory { get; private set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Creates an instance of simple population factory 
        /// with a default population size of 100
        /// </summary>
        public CoordinatesPopulationFactory()
            : this (100)
        {
        }

        /// <summary>
        /// Creates an instance of simple population factory
        /// with default population size given as parameter
        /// </summary>
        /// <param name="populationSize">population size</param>
        public CoordinatesPopulationFactory(int populationSize)
        {
            CLogger.LogTrace("Creating CoordinatesPopulationFactory.");
            this.PopulationSize = populationSize;
            this.SolutionFactory = new CoordinatesFactory();
        } 
        #endregion

        #region CreatePopulation
        public IPopulation<Coordinates> CreatePopulation()
        {
            CLogger.LogDebug("CoordinatesPopulationFactory -> CreatePopulation");
            CoordinatesPopulation population = new CoordinatesPopulation();
            for (int i = 0; i < PopulationSize; i++)
            {
                Coordinates coords = SolutionFactory.CreateInitializedSolution();
                population.Add(coords);
            }
            CLogger.LogDebug("Population created:\n{0}", population);
            return population;
        } 
        #endregion
    }
}
